
var servicesModule = angular.module('aaser.services', ['ngResource']);


servicesModule.factory('Assignment', function ($resource, AppConfig) {

    return $resource(AppConfig.restApiUrl + '/assignments/:Id',
        {Id: "@Id" });
});


/**
 * Authorization request for user's permissons
 */
servicesModule.factory('Authorization', function ($http, $resource, AppConfig, $location) {

    return function (params) {
        $http.get(AppConfig.restApiUrl + '/authorization', {params: params})
            .success(function (data, status, headers, config) {

            }).error(function (response) {
                $location.path("/login");
            });
    }
});
/**
 * Lock service prevents multiple refresh token sending.
 */
servicesModule.service('Lock', function () {
    this.unlock = true;

});
/**
 * Service for handling user's sessions
 */
servicesModule.service('Authentication', function ($resource, $location, $http, UserSession, authService, AppConfig, Lock) {
    function destroyForLogin() {
        authService.loginCancelled();
        UserSession.destroy();
        Lock.unlock = true;
        $location.path("/login");
    }

    this.refreshed = function (data) {
        UserSession.create(data);
        authService.loginConfirmed(null, function (config) {
            config.headers['Authorization'] = 'Bearer ' + UserSession.user.token.accessToken;
            return config;
        });
        Lock.unlock = true;
    };
    this.required = function () {
    };

    this.login = function (credentials,service) {
        $http.get(AppConfig.restApiUrl + '/authentication/'+ service, {
            headers: {'Authorization': 'Basic ' + credentials }
        }).success(function (data, status) {
            if (status === 200) {
                UserSession.create(data);
                Lock.unlock = true;
                $location.path("/");
            }
        });
    };

    this.cancel = function () {
        destroyForLogin();
    };

    return this;
});
/*
*Service for sending refresh tokens
 */
servicesModule.factory('Refresh', function ($http, UserSession, AppConfig, Authentication, Lock) {
    return function () {

        if (Lock.unlock) {
            if (UserSession.user) {
                if (UserSession.user.token.expires < Date.now()) {
                    Lock.unlock = false;
                    $http.get(AppConfig.restApiUrl + '/token-refresh?token=' + UserSession.user.token.refreshToken)
                        .success(function (data, status, headers, config) {
                            Authentication.refreshed(data);

                        }).error(function (response) {
                            Authentication.cancel();
                        });

                } else {
                    Authentication.cancel();
                }
            } else {
                Authentication.cancel();
            }

        }

    }
});

servicesModule.factory('Resources', function ($rootScope, $resource, AppConfig) {
    return {
        groups: $resource(AppConfig.restApiUrl + '/groups'),
        users: $resource(AppConfig.restApiUrl + '/users'),
        roles: $resource(AppConfig.restApiUrl + '/roles')

    }
});

